﻿using System;
using System.Windows;
using System.Windows.Input;
using BookSourceVerification.modelview;

namespace BookSourceVerification
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new VerifyBookSourceViewModel();
            if (!(App.filePaths?.Count > 0)) return;
            var verifyBookSourceViewModel = this.DataContext as VerifyBookSourceViewModel;
            var fIlePathList = verifyBookSourceViewModel!.FilePathList;
            foreach (var filePath in App.filePaths)
            {
                fIlePathList.Add(filePath);
            }
            App.filePaths.Clear();
            App.filePaths = null;
        }

        /// <summary>
        /// 文件拖入界面加载目录路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainWindow_OnDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetData(DataFormats.FileDrop) is not Array filePaths) return;
            var verifyBookSourceViewModel = this.DataContext as VerifyBookSourceViewModel;
            var fIlePathList = verifyBookSourceViewModel!.FilePathList;
            foreach (var filePath in filePaths)
            {
                if (filePath is string path)
                {
                    var itemCollection = fIlePathList;
                    if (itemCollection.Contains(path))
                    {
                        continue;
                    }

                    fIlePathList.Add(path);
                }
            }
        }


        private void MainWindow_OnPreviewDragEnter(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.Link;
            e.Handled = true;
            (sender as MainWindow)!.Cursor = Cursors.Arrow;
        }

        private void FrameworkElement_OnLoaded(object sender, RoutedEventArgs e)
        {
            var scrollViewer = this.ConsoleLogScrollViewer;
            scrollViewer.ScrollToBottom();
        }
    }
}